package com.snowflake.ecatalog.web.admin;

import java.util.Date;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.CompanyCategoryStandard;
import com.snowflake.ecatalog.service.admin.CompanyCategoryStandardService;
import com.snowflake.ecatalog.util.LoginUtil;

/**
 * Task管理的Controller, 使用Restful风格的Urls:
 * 
 * List page : GET /standard/ Create page : GET /standard/create Create action :
 * POST /standard/create Update page : GET /standard/update/{id} Update action :
 * POST /standard/update Delete action : GET /standard/delete/{id}
 * 
 */
@Controller
@RequestMapping(value = "/standard")
public class CompanyCategoryStandardController {

	private static final int PAGE_SIZE = 10;

	@Autowired
	private CompanyCategoryStandardService serveice;

	@RequestMapping(value = "")
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model,
			ServletRequest request) {
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		Page<CompanyCategoryStandard> standards = serveice.getCompanyCategoryStandards(searchParams, pageNumber,
				PAGE_SIZE);
		model.addAttribute("standards", standards);
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));

		return "admin/standardList";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("standard", new CompanyCategoryStandard());
		model.addAttribute("action", "create");
		return "admin/standardForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid CompanyCategoryStandard newStandard, RedirectAttributes redirectAttributes) {
		newStandard.setCreator(LoginUtil.getCurrentUser());
		newStandard.setCreatedTime(new Date());
		newStandard.setStatus(Status.ENABLE);
		serveice.saveCompanyCategoryStandard(newStandard);
		redirectAttributes.addFlashAttribute("message", "创建分类标准成功");
		return "redirect:/standard/";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("standard", serveice.getCompanyCategoryStandard(id));
		model.addAttribute("action", "update");
		return "admin/standardForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadCompanyCategoryStandard") CompanyCategoryStandard standard,
			RedirectAttributes redirectAttributes) {
		standard.setLastModifier(LoginUtil.getCurrentUser());
		standard.setLastModifiedTime(new Date());
		serveice.saveCompanyCategoryStandard(standard);
		redirectAttributes.addFlashAttribute("message", "更新分类标准成功");
		return "redirect:/standard/";
	}

	@RequestMapping(value = "enable/{id}")
	public String enable(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		serveice.enableCompanyCategoryStandard(id);
		redirectAttributes.addFlashAttribute("message", "启用分类标准成功");
		return "redirect:/standard/";
	}

	@RequestMapping(value = "disable/{id}")
	public String disable(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		serveice.disableCompanyCategoryStandard(id);
		redirectAttributes.addFlashAttribute("message", "禁用分类标准成功");
		return "redirect:/standard/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		// 校验分类标准下是否关联分类
		if (existCategoryInStandard(id)) {
			redirectAttributes.addFlashAttribute("message", "该分类标准下已关联分类，不能删除");
		} else {
			serveice.deleteCompanyCategoryStandard(id);
			redirectAttributes.addFlashAttribute("message", "删除分类标准成功");
		}
		return "redirect:/standard/";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出CompanyCategoryStandard对象
	 * ,再把Form提交的内容绑定到该对象上。 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadCompanyCategoryStandard")
	public CompanyCategoryStandard getCompanyCategoryStandard(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			CompanyCategoryStandard standard = serveice.getCompanyCategoryStandard(id);
			return standard;
		}
		return null;
	}

	private boolean existCategoryInStandard(Long standardId) {
		return serveice.existCategoryInStandard(standardId);
	}
}
